<?php
/**
 * DMCI Theme
 *
 *
 * Functions that are not pluggable (not wrapped in function_exists()) are instead attached
 * to a filter or action hook.
 *
 * For more information on hooks, actions, and filters, see http://codex.wordpress.org/Plugin_API.
 * 
 */

	include_once('inc/shortcodes.php');

/**
 * Menu Support
 */
 
register_nav_menus( 
	array(
		'primary' => __('Primary Navigation', 'dmcitheme'),
		'footer' => __('Footer Navigation', 'dmcitheme')
	) 
);

add_theme_support( 'menus' );
add_theme_support('post-thumbnails');

if ( function_exists('add_theme_support') ) 
{
    add_theme_support('menus');
}

/* Scripts and Styles */
function load_scripts_and_styles()
{
	global $post;
	wp_register_style( 'main-style', get_template_directory_uri() . '/style.css', array(), '20120208', 'all' ); 
	wp_register_style( 'dropKick-style', get_template_directory_uri() . '/css/dropkick.css', array(), '20120208', 'all' ); 
	wp_register_style( 'fancybox-style', get_template_directory_uri() . '/css/jquery.fancybox.css', array(), '20120208', 'all' ); 
	wp_enqueue_style( 'main-style' ); 
	wp_enqueue_style( 'dropKick-style' );
	wp_enqueue_style( 'fancybox-style' );
	
	// Register the script like this for a theme:
	
	wp_register_script( 'jQuery', get_template_directory_uri() . '/js/jquery-1.9.0.min.js' );
	wp_register_script( 'lightbox', get_template_directory_uri() . '/js/jquery.fancybox.js' );
	wp_register_script( 'dropkick', get_template_directory_uri() . '/js/jquery.dropkick-1.0.0.js' );
	wp_register_script( 'masonry', get_template_directory_uri() . '/js/jquery.masonry.min.js' );
	wp_register_script( 'scripts', get_template_directory_uri() . '/js/script.js' );
	

	// For either a plugin or a theme, you can then enqueue the script:
	wp_enqueue_script( 'jQuery' );
	wp_enqueue_script( 'lightbox' );
	wp_enqueue_script( 'dropkick' );
	wp_enqueue_script( 'masonry' );
	wp_enqueue_script( 'scripts' );
	
	
	
}

add_action( 'wp_enqueue_scripts', 'load_scripts_and_styles' );



function build_menu_array($post_type, $taxonomy, $slug){
	$args = array(
				'post_type' => $post_type,
				'tax_query' => array(
					array(
						'taxonomy' => $taxonomy,
						'field' => 'slug',
						'terms' => $slug
					)
				)
			);
	$menu_array = '';
	$query = new WP_Query( $args );
	while ( $query->have_posts() ) :
		$query->the_post();
		$menu_array .=  '[' . '\'' . get_the_title() . '\', ' . '\'' . get_permalink() . '\'],';
	endwhile;
	
	wp_reset_postdata();
	return '[' . substr_replace($menu_array, '', -1) . '];';
}

	
function get_project_category_posts($post_type, $taxonomy, $slug, $posts = false){
	
	$args = array(
				'post_type' => $post_type,
				'tax_query' => array(
					array(
						'taxonomy' => $taxonomy,
						'field' => 'slug',
						'terms' => $slug
					)
				)
			);
			
	if($posts) {
		$args['posts_per_page'] = $posts;
	}
	$query = new WP_Query( $args );
	
	return $query;
	
}

	add_action( 'admin_init', 'theme_options_init' );
	add_action( 'admin_menu', 'theme_options_add_page' ); 
	
	function theme_options_init(){
		register_setting( 'dmci_theme_options_settings', 'dmci_theme_options');
		
	} 
	

	function theme_options_add_page() {
	 	add_theme_page( 'Theme Options', 'Theme Options', 'edit_theme_options', 'theme_options', 'theme_options_do_page' );
	}
	
	function theme_options_do_page() { 
		global $select_options; if ( ! isset( $_REQUEST['settings-updated'] ) ) $_REQUEST['settings-updated'] = false;  ?>
		       
		<div class="wrap">
		<?php screen_icon(); echo "<h2>". __( 'Custom Theme Options', 'customtheme' ) . "</h2>";  ?>
		
		<?php if ( false !== $_REQUEST['settings-updated'] ) : ?>
		<div id="message" class="updated below-h2"><p>Options saved.</p></div>
		<?php endif; ?> 
		
		
		<form method="post" action="options.php">
		<?php settings_fields( 'dmci_theme_options_settings' ); ?>  
      
		<?php $options = get_option( 'dmci_theme_options' ); ?> 
		
		<p><strong>User Information</strong> </p>
		<?php wp_editor(!empty($options) ? $options['user_info'] : '', 'dmci_theme_options[user_info]');?>
		
		<p><strong>Contact Us Information</strong></p>
		<?php wp_editor( !empty($options) && isset($options['contact_info']) ? $options['contact_info'] : '', 'dmci_theme_options[contact_info]'); ?>
		
		<p><strong>Email:</strong> Place the email address where you want to send the contact form</p>
		<input type="text" name="dmci_theme_options[user_email]" size="30" value="<?php echo !empty($options) ? $options['user_email'] : ''; ?>" autocomplete="off">
		
		<p><strong>Social Links</strong></p>
		<hr/>
		<p>Please be sure to include <strong><?php echo esc_attr("http://") ?></strong> in the url.</p>
		<p><strong>Facebook URL</strong></p>
		<input type="text" name="dmci_theme_options[fb_url]" size="30" value="<?php echo !empty($options) ? $options['fb_url'] : ''; ?>" autocomplete="off">
		
		<p><strong>Twitter URL</strong></p>
		<input type="text" name="dmci_theme_options[twitter_url]" size="30" value="<?php echo !empty($options) ? $options['twitter_url'] : ''; ?>" autocomplete="off">
		
		<p><strong>Google Plus URL</strong></p>
		<input type="text" name="dmci_theme_options[gplus_url]" size="30" value="<?php echo !empty($options) ? $options['gplus_url'] : ''; ?>" autocomplete="off">
		<p><?php submit_button(); ?></p>
		</form>
		</div>
		<?php
	}
	
	function validate_email($e){
    	return (bool)preg_match("`^[a-z0-9!#$%&'*+\/=?^_\`{|}~-]+(?:\.[a-z0-9!#$%&'*+\/=?^_\`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?$`i", trim($e));
	}
	
	if ( function_exists( 'add_image_size' ) ) { 
		add_image_size( 'project-image-fullsize', 850, 9999 ); //300 pixels wide (and unlimited height)
		add_image_size( 'homepage-thumb', 220, 180, true ); //(cropped)
	}
	
	//Click Pagination
	function wp_infinitepaginate(){   
		
		$paged           = $_POST['page_no'];  
		$posts_per_page  = get_option('posts_per_page');  
	 
		# Load the posts  
		query_posts(
			array(
				'post_type'=> 'dmci_cons_update',
				'offset' => $paged,
				'posts_per_page' => 3,
				'meta_key' => 'cons_update_date',
				'orderby' => 'meta_value',
				'order' => 'DESC'
			)
		);
		while ( have_posts() ) : the_post();
			the_title();
			echo '<br/>';
		endwhile;
	  	
		exit;  
	}
	add_action('wp_ajax_infinite_scroll', 'wp_infinitepaginate');           // for logged in user  
	add_action('wp_ajax_nopriv_infinite_scroll', 'wp_infinitepaginate');
	/* End Click Pagination */
	

	?>